package nl.tue.be.ds.duraark.bSDD.ontology;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;

import nl.tue.be.ds.duraark.bSDD.DataBaseStorageUnit;
import nl.tue.be.ds.duraark.bSDD.DataBaseUtilities;

import com.db4o.ObjectContainer;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class OntoGenerator {

	public static final String ontoNamespace = "http://www.tue.nl/ddss/duraark#";
	private HashSet<DataBaseStorageUnit> dbSunitSet;
	private final String ns0 = "http://bsdd.peregrine.catenda.com/ns0#";
	private ObjectContainer db = DataBaseUtilities.db;

	public void ontoPrcoecess() {

		OntModel ontModel = ModelFactory
				.createOntologyModel(OntModelSpec.OWL_MEM);
		ontModel.setNsPrefix("bsdd", ontoNamespace);
		ontModel.setNsPrefix("ns0", ns0);

		dbSunitSet = DataBaseUtilities.getAllDumpedDBSunit(db);

		Iterator<DataBaseStorageUnit> setIter = dbSunitSet.iterator();
		if (setIter.hasNext()) {
			DataBaseStorageUnit dbSunit = setIter.next();
			OntClass ontoClass = ontModel.createClass(ontoNamespace
					+ dbSunit.getGuid());
			PropertyProcessor propertyProcessor = new PropertyProcessor(
					ontModel, ontoClass, ns0);
			ClassProcessor cp = new ClassProcessor(ontModel, ontoClass, ns0,
					propertyProcessor);
			cp.classProcess(dbSunit);
		}
		ontModel.write(System.out,"Turtle");
		
		FileWriter out = null;
		try {
			out = new FileWriter("bSDD.ttl");
			ontModel.write(out, "Turtle");
		} catch (IOException e) {

			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {

					e.printStackTrace();
				}
			}
		}

	}

}
